<?php

namespace app\index\controller;

use think\Controller;
use app\index\model\Index as mIndex;
use think\Request;
use app\index\controller\Base;

class Search extends Base
{
    public function index()
    {              
        return view('search/search', ['list' => $this->list]);
    }

    // 搜索主页ajax请求
    public function ajax()
    {
        $model = model('Index');
        $data = $model->index();

        // 判断有没有头像
        foreach ($data as $k => $v) {
            if($v['icon']==null){
                $data[$k]['icon'] = '/static/admin/img/default.jpg';
            }
        }
        return json($data);
    }

    // 最近登录请求
    public function recentlyLogged()
    {
        $search = model('Search');
        $res = $search->recentlyLogged();

        foreach($res as $k => $v){
            if($v['icon']== null){
                $res[$k]['icon'] = '/static/admin/img/default.jpg';
            }

            if($v['birthday'] == null){
                $res[$k]['birthday'] = '保密';
            }
        }
        return json($res);
    }

    // 最新注册请求
    public function latestReg()
    {
        $search = model('Search');
        $res = $search->latestReg();

        foreach($res as $k => $v){
            if($v['icon']== null){
               $res[$k]['icon'] = '/static/admin/img/default.jpg';
            }

            if($v['birthday'] == null){
                $res[$k]['birthday'] = '保密';
            }
        }
        return json($res);
    }

    // 搜索
    public function commit()
    {
        $data = Request::instance()->get();
        $t = date('Y', time());
        // 出生年
        $year = $t - $data['age'];

        // 收入
        $salary = $data['salary'];

        $search = model('Search');

        if($data['age'] != null){
            $res = $search->condition($salary, $year);
        } else{
            $res = $search->condition($salary);
        }

        if(empty($res)){
            $data = [
                'status'=>false,
                'msg'=>'暂无数据',
            ];
            return json($data);
        } else {
            foreach($res as $k => $v){
                if($v['icon']== null){
                    $res[$k]['icon'] = '/static/admin/img/default.jpg';
                }

                if($v['birthday'] == null){
                    $res[$k]['birthday'] = '保密';
                }
            }
            $data = [
                'status' => true,
                'msg' => $res,
            ];
            return json($data);
        }
    }
}

